package com.ruoyi.web.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.juhang.domain.Post;
import com.ruoyi.juhang.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/app/index")
public class AppIndexController {

    @Autowired
    private IPostService postService;

    /**
     * 获取职位列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPostList")
    public AjaxResult getPostList(String title, String city, Integer type, Integer userId,Integer pageNo, Integer pageSize){
        IPage<Post> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(title)){
            queryWrapper.like("title",title);
        }
        if(StringUtils.isNotEmpty(city) && !city.equals("全部")){
            queryWrapper.like("city",city);
        }
        if(type > -1){
            queryWrapper.eq("type",type);
        }
        queryWrapper.eq("state",0).orderByAsc("sort");
        IPage<Post> postIPage = postService.getPostList(page, queryWrapper, userId);
        return AjaxResult.success(postIPage);
    }

    /**
     * 获取城市职位数据
     * @return
     */
    @GetMapping("/getPostGroupCity")
    public AjaxResult getPostGroupCity(String title, Integer type){
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(title)){
            queryWrapper.like("title",title);
        }
        if(type > -1){
            queryWrapper.eq("type",type);
        }
        queryWrapper.eq("state",0).groupBy("city");
        return AjaxResult.success(postService.getPostGroupCity(queryWrapper));
    }

    /**
     * 获取职位详情
     * @param postId
     * @return
     */
    @GetMapping("/getPostById")
    public AjaxResult getPostById(Integer postId, Integer userId){
        List<Post> list = postService.getPostById(new QueryWrapper<Post>().eq("state", 0).eq("id", postId), userId);
        return AjaxResult.success(list.get(0));
    }
}
